<?php namespace App\Http\Controllers\user\industry;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Illuminate\Pagination\LengthAwarePaginator;
use Landish\Pagination\Pagination as BootstrapPresenter;

use App\Models\Projects;
use App\Models\Domains;
use App\Models\Techs;
use App\Models\Devices;
use App\Models\Technologies;
use Input;
use DB;
use App\Models\Collecttion;

class TechController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$numPerPage = 12;
		$techsList = Technologies::where("verify_state", 1)->orderBy("id", "desc");

		if(Input::has("aid")){
			$techsList->whereIn("id", DB::table("tech_domain")->whereIn("cate_id", DB::table("domains")->where("parent_id", Input::get("aid"))->lists("id"))->lists("tech_id"))->orWhereIn("id", DB::table("tech_domain")->where("cate_id", Input::get("aid"))->lists("tech_id"));
		}

		if(Input::has("cid"))
		{
			$techsList->whereIn("id", DB::table("tech_domain")->where("cate_id", Input::get("cid"))->lists("tech_id"));
		}

		if(Input::has("tid"))
		{
			$techsList->whereIn("id", DB::table("tech_tech")->where("cate_id", Input::get("tid"))->lists("tech_id"));
		}

		$count = $techsList->count();

		$curPage = (int)Input::get("page", 1);
		$p = new LengthAwarePaginator($techsList, $count, $numPerPage, $curPage);
		$p->setPath("");
        $presenter = new BootstrapPresenter($p);

		$domains = Domains::with(["children" => function($query){
			$query->where("parent_id", Input::get("aid", 1));
		}])->where("parent_id", 0)->get()->toArray();
		$techs = Techs::all();

		$techsList = $techsList->skip(($curPage - 1) * $numPerPage)->take($numPerPage)->get();

		$recTechs = Technologies::where("verify_state", 1)->where("is_rec", 1)->orderBy("order", "desc")->skip(($curPage - 1) * 5)->take(5)->get();

		$bottomNumPerPage = 6;
		$bottomProject = Projects::with(["projectDomain", "projectDomain.domain", "projectDomain.domain.parent", "projectTech", "projectTech.tech", "investmentType"])->where("verify_state", 1)->where("is_rec_list_bottom", 1)->skip(($curPage - 1) * $bottomNumPerPage)->take(6)->get();
		$bottomDevice = Devices::where("verify_state", 1)->where("is_rec_list_bottom", 1)->skip(($curPage - 1) * $bottomNumPerPage)->take(6)->get();
		$bottomTech = Technologies::where("verify_state", 1)->where("is_rec_list_bottom", 1)->skip(($curPage - 1) * $bottomNumPerPage)->take(6)->get();

		$queryStr = "";
		foreach (Input::all() as $key => $value)
		{
			$queryStr .= $key . "=" . $value . "&";
		}

		$params = ["techsList" => $techsList, "count" => $count, "domains" => $domains, "techs" => $techs, "recommand" => $recTechs, "bottomProject" => $bottomProject, "bottomDevice" => $bottomDevice, "bottomTech" => $bottomTech, "bottomTech" => $bottomTech, "curQuery" => $queryStr];

		if(Input::has("aid")){
			$firstLevel = Domains::where("id", Input::get("aid"));
			if($firstLevel->count()){
				$params["firstLevel"] = $firstLevel->get()[0]["cate_name"];
			}
		}

		if(Input::has("cid")){
			$secondLevel = Domains::where("id", Input::get("cid"));
			if($secondLevel->count()){
				$params["secondLevel"] = $secondLevel->get()[0]["cate_name"];
			}
		}

		if(Input::has("tid")){
			$thirdLevel = Techs::where("id", Input::get("tid"));
			if($thirdLevel->count())
			{
				$params["thirdLevel"] = $thirdLevel->get()[0]["cate_name"];
			}
		}

		return view('user.industry.tech', $params)->withPresenter($presenter)->withIsTech(true)->withIsIndustry(true);
	}

	public function showDetail()
	{
		$tid = (int)Input::get('tid',0);
		if($tid <= 0){
			return App::abort(404);
		}
		$model = Technologies::with(["user", "techDomain", "techDomain.domain", "techDomain.domain.parent", "techCases", "techTech", "techScope", "techProject", "techProject.projects"])->where("verify_state", 1)->where('id', $tid);

		if($model->count()){
			$model = $model->get()[0];
			switch($model["user"]["role_id"]){
				case env("ENERGY"):
					$model->load("user.poweruser");
					$model["userData"] = $model["user"]["poweruser"];
					unset($model["user"]["poweruser"]);
				break;
				case env("SAVER"):
					$model->load("user.energysaver");
					$model["userData"] = $model["user"]["energysaver"];
					unset($model["user"]["energysaver"]);
				break;
				case env("DEVICE_TECH"):
					$model->load("user.devicetech");
					$model["userData"] = $model["user"]["devicetech"];
					unset($model["user"]["devicetech"]);
				break;
				case env("THIRD"):
					$model->load("user.thirdparty");
					$model["userData"] = $model["user"]["thirdparty"];
					unset($model["user"]["thirdparty"]);
				break;
				case env("ASSOCIATION"):
					$model->load("user.association");
					$model["userData"] = $model["user"]["association"];
					unset($model["user"]["association"]);
				break;
				case env("GOVERNMENT"):
					$model->load("user.government");
					$model["userData"] = $model["user"]["government"];
					unset($model["user"]["government"]);
				break;
			}
			$model = $model->toArray();
		}else{
			return App::abort(404);
		}
		$uid = $this->getUid();
		$isCollect = Collecttion::where("type", 2)->where("relate_id", $tid)->where("uid", $uid)->count();
		return view('user.industry.tech_detail', ["model" => $model, "isCollect" => $isCollect])->withIsIndustry(true);
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

	public function techList()
	{
		return view("user.industry_tech_list");
	}

}
